from rest_framework import serializers
from .models import User

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'created_at')
        read_only_fields = ('created_at',)

class UserRegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email', 'password']
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        # 直接创建用户（绕过密码哈希）
        return User.objects.create(
            username=validated_data['username'],
            email=validated_data['email'],
            password=validated_data['password']  # 明文存储
        )